home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH8 / SRC / CHAOS.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  5.2 KB  |  184 lines

  1. VERSION 4.00
  2. Begin VB.Form ChaosForm 
  3.    Caption         =   "Chaos Game"
  4.    ClientHeight    =   4575
  5.    ClientLeft      =   2280
  6.    ClientTop       =   1185
  7.    ClientWidth     =   5535
  8.    Height          =   5265
  9.    Left            =   2220
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   305
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   369
  14.    Top             =   555
  15.    Width           =   5655
  16.    Begin VB.PictureBox Canvas 
  17.       AutoRedraw      =   -1  'True
  18.       FillStyle       =   0  'Solid
  19.       Height          =   4560
  20.       Left            =   960
  21.       ScaleHeight     =   300
  22.       ScaleMode       =   3  'Pixel
  23.       ScaleWidth      =   300
  24.       TabIndex        =   1
  25.       Top             =   0
  26.       Width           =   4560
  27.    End
  28.    Begin VB.CommandButton CmdGo 
  29.       Caption         =   "Go"
  30.       Default         =   -1  'True
  31.       Enabled         =   0   'False
  32.       Height          =   495
  33.       Left            =   120
  34.       TabIndex        =   0
  35.       Top             =   120
  36.       Width           =   735
  37.    End
  38.    Begin MSComDlg.CommonDialog FileDialog 
  39.       Left            =   240
  40.       Top             =   1440
  41.       _version        =   65536
  42.       _extentx        =   847
  43.       _extenty        =   847
  44.       _stockprops     =   0
  45.       cancelerror     =   -1  'True
  46.    End
  47.    Begin VB.Menu mnuFile 
  48.       Caption         =   "&File"
  49.       Begin VB.Menu mnuFileLoad 
  50.          Caption         =   "&Load..."
  51.          Shortcut        =   ^L
  52.       End
  53.       Begin VB.Menu mnuFileSep 
  54.          Caption         =   "-"
  55.       End
  56.       Begin VB.Menu mnuFileExit 
  57.          Caption         =   "E&xit"
  58.       End
  59.    End
  60. Attribute VB_Name = "ChaosForm"
  61. Attribute VB_Creatable = False
  62. Attribute VB_Exposed = False
  63. Option Explicit
  64. Dim NumAnchors As Integer
  65. Dim AnchorX() As Single
  66. Dim AnchorY() As Single
  67. Dim Running As Boolean
  68. ' ************************************************
  69. ' Draw the anchor points.
  70. ' ************************************************
  71. Sub DrawAnchors()
  72. Const GAP = 2
  73. Dim i As Integer
  74.     Canvas.Cls
  75.     For i = 1 To NumAnchors
  76.         Canvas.Line _
  77.             (AnchorX(i) - GAP, AnchorY(i) - GAP)- _
  78.             Step(2 * GAP, 2 * GAP), , BF
  79.     Next i
  80. End Sub
  81. ' ************************************************
  82. ' Load the anchor points.
  83. ' ************************************************
  84. Sub LoadChaosData(fname As String)
  85. Dim fnum As Integer
  86. Dim i As Integer
  87.     fnum = FreeFile
  88.     Open fname For Input Access Read As #fnum
  89.     Input #fnum, NumAnchors
  90.     ReDim AnchorX(1 To NumAnchors)
  91.     ReDim AnchorY(1 To NumAnchors)
  92.     For i = 1 To NumAnchors
  93.         Input #fnum, AnchorX(i), AnchorY(i)
  94.     Next i
  95.     Close #fnum
  96.     DrawAnchors
  97.     Caption = "Chaos Game [" & fname & "]"
  98.     CmdGo.Enabled = True
  99. End Sub
  100. ' ************************************************
  101. ' Play the chaos game.
  102. ' ************************************************
  103. Sub PlayGame()
  104. Dim wid As Single
  105. Dim hgt As Single
  106. Dim x As Single
  107. Dim y As Single
  108. Dim anchor As Integer
  109. Dim i As Integer
  110.     ' See how much room we have.
  111.     wid = Canvas.ScaleWidth
  112.     hgt = Canvas.ScaleHeight
  113.     ' Pick a random starting point.
  114.     x = wid * Rnd
  115.     y = hgt * Rnd
  116.     ' Start the game.
  117.     i = 0
  118.     Do While Running
  119.         ' Pick a random anchor point.
  120.         anchor = Int(NumAnchors * Rnd + 1)
  121.         ' Move halfway there.
  122.         x = (x + AnchorX(anchor)) / 2
  123.         y = (y + AnchorY(anchor)) / 2
  124.         Canvas.PSet (x, y)
  125.         ' To make things faster, only DoEvents
  126.         ' every 100 times.
  127.         i = i + 1
  128.         If i > 100 Then
  129.             i = 0
  130.             DoEvents
  131.         End If
  132.     Loop
  133. End Sub
  134. Private Sub CmdGo_Click()
  135. Dim i As Integer
  136.     If Running Then
  137.         Running = False
  138.         CmdGo.Enabled = False
  139.         CmdGo.Caption = "Stopped"
  140.     Else
  141.         Running = True
  142.         CmdGo.Caption = "Stop"
  143.         DrawAnchors
  144.         PlayGame
  145.         CmdGo.Enabled = True
  146.         CmdGo.Caption = "Go"
  147.     End If
  148. End Sub
  149. Private Sub Form_Resize()
  150.     Canvas.Move Canvas.Left, 0, _
  151.         ScaleWidth - Canvas.Left, ScaleHeight - 1
  152. End Sub
  153. Private Sub Form_Unload(Cancel As Integer)
  154.     End
  155. End Sub
  156. Private Sub mnuFileExit_Click()
  157.     Unload Me
  158. End Sub
  159. ' ************************************************
  160. ' Load a file describing the anchor points.
  161. ' ************************************************
  162. Private Sub mnuFileLoad_Click()
  163. Dim fname As String
  164.     ' Allow the user to pick a file.
  165.     On Error Resume Next
  166.     FileDialog.FilterIndex = 1
  167.     FileDialog.filename = "*.CHA"
  168.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  169.     FileDialog.ShowOpen
  170.     If Err.Number = cdlCancel Then
  171.         Exit Sub
  172.     ElseIf Err.Number <> 0 Then
  173.         Beep
  174.         MsgBox "Error selecting file.", , vbExclamation
  175.         Exit Sub
  176.     End If
  177.     On Error GoTo 0
  178.     fname = Trim$(FileDialog.filename)
  179.     FileDialog.InitDir = Left$(fname, Len(fname) _
  180.         - Len(FileDialog.FileTitle) - 1)
  181.     ' Load the information.
  182.     LoadChaosData fname
  183. End Sub
  184.